package com.xzj.admin.resource.analysis.handle.groovy;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.xzj.admin.analysis.base.domain.ResMessageManage;
import com.xzj.admin.resource.analysis.cache.GroovyScriptCache;
import com.xzj.admin.resource.analysis.domain.vo.ScriptReqVo;
import groovy.lang.Script;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @program: xzj-admin
 * @ClassName: GroovyScriptHandle
 * @description: Groovy脚本执行类
 * @author: XiaZunJun
 * @create: 2024-10-28 17:20
 */
@Component
@Slf4j
public class GroovyScriptHandle {

    /**
     * 测试ID模转groovy脚本
     * @author XiaZunJun
     * @date 2024/10/28 下午5:27
     * @param scriptReqVo 脚本请求对象
     *
     */
    public void testConvertId(ScriptReqVo scriptReqVo){
        ResMessageManage resMessageManage = new ResMessageManage();
        JSONObject row = new JSONObject();
        row.put("id","1001");
        row.put("name","testName");
        row.put("code","1001001");
        resMessageManage.setMessageJson(row.toJSONString());
        resMessageManage.setSourceId("1");
        resMessageManage.setRowData(row);
        Script script = GroovyScriptCache.loadScript(scriptReqVo.getScriptCode(), scriptReqVo.getScriptContent());
        script.invokeMethod(scriptReqVo.getScriptCode(), resMessageManage);
        log.info("resMessageManage is {}", JSON.toJSONString(resMessageManage));
    }


}
